﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion


#region Usings

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using Microsoft.Surface.Presentation;
using ScrumTable.BL.DM.DataManagement;
using ScrumTable.UI.View.Controls;
using ScrumTable.UI.View.Extension;
using ScrumTable.UI.ViewModel;

#endregion

namespace ScrumTable.UI.View.WinTouch.UserControls.ItemControls
{
 

    /// <summary>
    /// Interaction logic for ContextMenu.xaml
    /// </summary>
    public partial class ListClickControl  
    {
         
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

 
        
        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

         
        /// <summary>
        /// DependencyProperty for Text
        /// </summary>
        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text",
                                        typeof(string),
                                        typeof(ListClickControl),
                                        new UIPropertyMetadata(null));
        /// <summary>
        /// Gets or sets the text on the control
        /// </summary>
        /// <value>The text shown on the control.</value>
        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }


        /// <summary>
        /// DependencyProperty for Text
        /// </summary>
        public static readonly DependencyProperty ColorProperty =
            DependencyProperty.Register("Color",
                                        typeof(Brush),
                                        typeof(ListClickControl),
                                        new UIPropertyMetadata(null));
        /// <summary>
        /// Gets or sets the brush for the click background
        /// </summary>
        /// <value>brush for the click background</value>
        public Brush Color
        {
            get { return (Brush)GetValue(ColorProperty); }
            set { SetValue(ColorProperty, value); }
        }


        /// <summary>
        /// DependencyProperty for Text
        /// </summary>
        public static readonly DependencyProperty CanClickProperty =
            DependencyProperty.Register("CanClick",
                                        typeof(Func<object, ScrumState, CanDropResult>),
                                        typeof(ListClickControl),
                                        new UIPropertyMetadata(null, OnCanClickPropertyChangedCallback));
        /// <summary>
        /// Gets or sets the brush for the click background
        /// </summary>
        /// <value>brush for the click background</value>
        public Func<object, ScrumState, CanDropResult> CanClick
        {
            get { return (Func<object, ScrumState, CanDropResult>)GetValue(CanClickProperty); }
            set { SetValue(CanClickProperty, value); }
        }


        /// <summary>
        /// DependencyProperty for Text
        /// </summary>
        public static readonly DependencyProperty ViewModelProperty =
            DependencyProperty.Register("ViewModel",
                                        typeof(IScrumItemViewModel<DomainBase>),
                                        typeof(ListClickControl),
                                        new UIPropertyMetadata(null, OnViewModelPropertyChangedCallback));
        /// <summary>
        /// Gets or sets the brush for the click background
        /// </summary>
        /// <value>brush for the click background</value>
        public IScrumItemViewModel<DomainBase> ViewModel
        {
            get { return (IScrumItemViewModel<DomainBase>)GetValue(ViewModelProperty); }
            set { SetValue(ViewModelProperty, value); }
        }

        /// <summary>
        /// DependencyProperty for Text
        /// </summary>
        public static readonly DependencyProperty CanClickOpacityMaskProperty =
            DependencyProperty.Register("CanClickOpacityMask",
                                        typeof(Brush),
                                        typeof(ListClickControl),
                                        new UIPropertyMetadata(null));
        /// <summary>
        /// Gets or sets the brush for the click background
        /// </summary>
        /// <value>brush for the click background</value>
        public Brush CanClickOpacityMask
        {
            get { return (Brush)GetValue(CanClickOpacityMaskProperty); }
            set { SetValue(CanClickOpacityMaskProperty, value); }
        }

        
        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

         
        public ListClickControl()
        {
            InitializeComponent();
                  
        }

        protected override void OnPreviewMouseDown(MouseButtonEventArgs e)
        {
            base.OnPreviewMouseDown(e);
            CheckifCanClick(e);
        }

        protected override void OnPreviewTouchDown(TouchEventArgs e)
        {
            base.OnPreviewTouchDown(e);
            CheckifCanClick(e);
        }

        private void addHandlerToViewModel()
        {
            ViewModel.PropertyChanged += OnStateChange;
            SetOpacityMask();
        }

        private static void OnCanClickPropertyChangedCallback(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            if (args.NewValue != null)
            {
                ((ListClickControl)sender).SetOpacityMask();
            }
        }


         private static void OnViewModelPropertyChangedCallback(DependencyObject sender, DependencyPropertyChangedEventArgs args)
         {
             if (args.NewValue != null)
             {
                 ((ListClickControl) sender).addHandlerToViewModel();
             }
         }

         void OnStateChange(object sender, System.ComponentModel.PropertyChangedEventArgs e)
         {
             if( e.PropertyName == "State")
             {
                 SetOpacityMask();
             }
         }


        #endregion



         private void SetOpacityMask()
         {
             if (CanClick != null && ViewModel != null)
             {
                 if (CanClick(ViewModel, this.DataContext as ScrumState) != CanDropResult.CanDrop)
                 {
                     CanClickOpacityMask = Application.Current.Resources["disabledMaskLinar"] as Brush;
                 }
                 else
                 {
                     CanClickOpacityMask = null;
                 }
             }
         }

        private void CheckifCanClick(RoutedEventArgs e)
        {
            if (CanClick != null)
            {
                e.Handled = CanClick(ViewModel, this.DataContext as ScrumState) != CanDropResult.CanDrop;
            }
        }
    }
}
